package es.mgil.whoWantsToBeAMillonaire.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * Utility class for the application
 * 
 * @author marc
 */
public class Utils {
	private static List<Integer> awardList;

	/**
	 * Get the list of awards. This list is stored into a cached list in order
	 * to prevent to calculate it every time
	 */
	public static List<Integer> fillAwards() {
		if (awardList == null) {
			awardList = new ArrayList<Integer>();

			awardList.add(0);
			awardList.add(100);
			awardList.add(200);
			awardList.add(300);
			awardList.add(500);
			awardList.add(1000);
			awardList.add(2000);
			awardList.add(4000);
			awardList.add(8000);
			awardList.add(16000);
			awardList.add(32000);
			awardList.add(64000);
			awardList.add(125000);
			awardList.add(250000);
			awardList.add(500000);
			awardList.add(1000000);
		}

		return awardList;
	}

	private static List<Question> questionList;

	/**
	 * Read the list of questions from an XML file. The list of questions is
	 * stored into a cached list in order to have it loaded only once
	 */
	public static List<Question> getQuestionsList(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		if (questionList == null) {
			questionList = new ArrayList<Question>();

			try {
				int eventType = XmlPullParser.START_DOCUMENT;

				while (eventType != XmlPullParser.END_DOCUMENT) {
					if (eventType == XmlPullParser.START_TAG
							&& parser.getName().equals("question")) {
						String answer1 = parser.getAttributeValue(null,
								"answer1");
						String answer2 = parser.getAttributeValue(null,
								"answer2");
						String answer3 = parser.getAttributeValue(null,
								"answer3");
						String answer4 = parser.getAttributeValue(null,
								"answer4");
						String audience = parser.getAttributeValue(null,
								"audience");
						String fifty1 = parser
								.getAttributeValue(null, "fifty1");
						String fifty2 = parser
								.getAttributeValue(null, "fifty2");
						String number = parser
								.getAttributeValue(null, "number");
						String phone = parser.getAttributeValue(null, "phone");
						String right = parser.getAttributeValue(null, "right");
						String text = parser.getAttributeValue(null, "text");

						Question q = new Question();
						q.setAnswer1(answer1);
						q.setAnswer2(answer2);
						q.setAnswer3(answer3);
						q.setAnswer4(answer4);
						q.setAudience(audience);
						q.setFifty1(fifty1);
						q.setFifty2(fifty2);
						q.setNumber(number);
						q.setPhone(phone);
						q.setRight(right);
						q.setText(text);

						questionList.add(q);
					}
					eventType = parser.next();
				}
			} catch (XmlPullParserException e1) {
				throw e1;
			} catch (IOException e2) {
				throw e2;
			}
		}

		return questionList;
	}
}
